<template>
	<view class="container">
		<u--form :model="formModel" ref="fromRules" label-width='100'>
			<u-form-item label="头像" borderBottom @click='upload'>
				<image style="width: 140rpx;height: 140rpx;border-radius: 20rpx;" :src="formModel.avatarUrl ? formModel.avatarUrl : 'https://ky.railshj.cn/protocol/icon/images/mine/mine_avatar.png'"
				 mode="aspectFill"></image>
			</u-form-item>
			<u-form-item label="昵称" prop="nickname" borderBottom rightIcon='arrow-right'><u--input v-model="formModel.nickname" placeholder='请输入昵称' maxlength='8' border="none"/></u-form-item>
			<u-form-item label="真实姓名" prop="realname" borderBottom><u-input v-model="formModel.realname" placeholder='请输入昵称' maxlength='20' border="none"/></u-form-item>
			<u-form-item label="性别" prop="genderText" borderBottom @click='genderActionShow = true'><u-input v-model="formModel.genderText" placeholder='请选择性别' disabled disabledColor='#fff' border="none"/></u-form-item>
			<!-- <u-form-item label="出生日期" prop="birthday" :required='true'><u-input v-model="formModel.birthday" placeholder='请选择出生日期' @click='dateActionShow = true' type="select" /></u-form-item> -->
			<!-- <view class="tips"><text>岁</text> <text>sds</text></view> -->
			<!-- <u-form-item label="公司性质" prop="unit"><u-input v-model="formModel.unit" placeholder='请选择单位' @click='selectUnit' type="select" /></u-form-item> -->
			<!-- <u-form-item label="公司性质" prop="business"><u-input v-model="formModel.business" placeholder='请输入公司性质' maxlength='20'/></u-form-item> -->
			<!-- <u-form-item label="职业职务" prop="job"><u-input v-model="formModel.job" placeholder='请输入职位' maxlength='20'/></u-form-item> -->
			<!-- <u-form-item label="身高(cm)" prop="height" :required='true'><u-input v-model="formModel.height" placeholder='请选择身高' @click='heightActionShow = true' type="select" /></u-form-item> -->
			<!-- <u-form-item label="体重(kg)" prop="weight"><u-input v-model="formModel.weight" placeholder='请选择体重' @click='weightActionShow = true' type="select" /></u-form-item> -->
			<u-form-item label="所在城市" prop="city" borderBottom @click='selectCity'><u--input v-model="formModel.city" placeholder='请选择' disabled disabledColor='#fff' border="none"/></u-form-item>
			<!-- <u-form-item label="家乡"><u-input v-model="formModel.area" placeholder='请选择家乡' @click='selectArea' type="select"/></u-form-item> -->
			<!-- <u-form-item label="学历" prop="education"><u-input v-model="formModel.education" placeholder='请选择学历' @click='educationActionShow = true' type="select"/></u-form-item> -->
			<!-- <u-form-item label="感情状况" prop="marriage"><u-input v-model="formModel.marriage" placeholder='请选择感情状况' @click='marriageActionShow = true' type="select"/></u-form-item> -->
			<!-- <u-form-item label="微信" prop="weixin"><u-input v-model="formModel.weixin" placeholder='请输入微信号' /></u-form-item> -->
		</u--form>
		<!-- 性别选择器 -->
		<u-action-sheet :actions="genderList" :show="genderActionShow" @select="genderAction" :closeOnClickOverlay='true' :closeOnClickAction="true" cancelText='取消' @close='genderActionShow = false'></u-action-sheet>
		<!-- 日期选择器 -->
		<!-- <u-picker v-model="dateActionShow" @confirm='dateAction' mode="time" :default-time='defaultTime'></u-picker> -->
		<!-- 身高选择器 -->
		<!-- <u-select v-model="heightActionShow" @confirm='heightAction' :list='heightList' :default-value='heightDefauleValue'></u-select> -->
		<!-- 体重选择器 -->
		<!-- <u-select v-model="weightActionShow" @confirm='weightAction' :list='weightList' :default-value='weightDefauleValue'></u-select> -->
		<!-- 学历选择器 -->
		<!-- <u-select v-model="educationActionShow" @confirm='educationAction' :list='educationList' :default-value='educationDefauleValue'></u-select> -->
		<!-- 婚姻选择器 -->
		<!-- <u-select v-model="marriageActionShow" @confirm='marriageAction' :list='marriageList' :default-value='marriageDefauleValue'></u-select> -->
		<!-- 地区选择器 -->
		<simple-address ref="simpleAddress" :pickerValueDefault="cityPickerValueDefault" @onConfirm="onConfirm" themeColor='#007AFF'></simple-address>
		<!-- 家乡选择器 -->
		<!-- <simple-address ref="simpleAddressForArea" :pickerValueDefault="cityPickerValueDefault" @onConfirm="onConfirmForArea" themeColor='#007AFF'></simple-address> -->
		<!-- 工作单位 -->
		<!-- <u-select v-model="workUnitShow" :list="workUnitList" :default-value='workUnitDefaultValue' @confirm='workUnitConfirm'></u-select> -->
		<view class="footer">
			<jc-button :width="600" title='保 存' @click='submit' backgroundColor="#d4a781"></jc-button>
		</view>
	</view>
</template>

<script>
	import {
		user
	} from '@/api/user.js'
	import simpleAddress from '@/components/simple-address/simple-address.vue';
	import {uploadFileToService} from '@/common/utils/uploadFile.js'
	export default{
		components:{ simpleAddress},
		data() {
			return {
				userInfo: {},
				formModel: {},
				fromRules: {
				},
				genderActionShow: false,
				genderList:[{ name: '男', gender: 'M' }, { name: '女', gender: 'W' }],
				dateActionShow: false,
				heightActionShow: false,
				weightActionShow: false,
				educationActionShow: false,
				marriageActionShow: false,
				cityPickerValueDefault: [0, 0, 1],
				heightList: [],
				heightDefauleValue: [35],
				weightList: [],
				weightDefauleValue: [35],
				educationList: [{value: 1, label: '大专以下'},{value: 2, label: '大专'},{value: 3, label: '本科'},{value: 4, label: '硕士及以上'},{value: 5, label: '不选'}],
				educationDefauleValue: [3],
				marriageList: [{value: 1, label: '单身'},{value: 2, label: '恋爱中'},{value: 3, label: '已婚'}],
				marriageDefauleValue: [1],
				workUnitShow: false,
				workUnitList: [],
				workUnitDefaultValue: [],
				defaultTime: ''
			}
		},
		onLoad() {
			this.getUserInfo()
		},
		onShow() {
			this.getHeightList()
			this.getWeightList()
		},
		onReady() {
			this.$refs.fromRules.setRules(this.fromRules)
		},
		methods:{
			getHeightList(){
				for(var i=0; i<=80; i++){
					var item = {}
					item.label = i+140
					item.value = i
					this.heightList.push(item)
				}
			},
			getWeightList(){
				for(var i=0; i<=80; i++){
					var item = {}
					item.label = i+40
					item.value = i
					this.weightList.push(item)
				}
			},
			upload(){
				uploadFileToService().then(res=>{
					if(res.code==10000){
						this.formModel.avatarUrl = res.data.url
					}
				})
			},
			//性别选择
			genderAction(e){
				console.log('性别', e)
				this.formModel.genderText = e.name
				this.formModel.gender = e.gender
			},
			//日期选择
			dateAction(date){
				console.log('日期', date)
				this.formModel.birthday = date.year+'-'+date.month+'-'+date.day
				this.formModel.age = this.$tools.getAge(this.formModel.birthday)
				this.formModel.constellation = this.$tools.getAstro(date.month, date.day)
			},
			//选择身高
			heightAction(e){
				console.log('身高', e)
				this.heightDefauleValue = [String(e[0].value)]
				this.formModel.height = String(e[0].label)
			},
			//选择体重
			weightAction(e){
				console.log('体重', e)
				this.weightDefauleValue = [e[0].value]
				this.formModel.weight = e[0].label
			},
			//选择学历
			educationAction(e){
				console.log('学历', e[0])
				this.educationDefauleValue = [e[0].value]
				if(e[0].value == 5){
					this.formModel.education = ''
				}else{
					this.formModel.education = e[0].label
				}
			},
			//选择婚姻状况
			marriageAction(e){
				console.log('婚姻状况', e[0].label)
				this.marriageDefauleValue = [e[0].value]
				this.formModel.marriage = e[0].label
			},
			//选择所在地
			selectCity(){
				this.$refs.simpleAddress.open()
			},
			//选择所在地回调
			onConfirm(e) {
				console.log('选择现居地', e)
				var city = e.labelArr[1]=='市辖区'?'':e.labelArr[1]
				this.formModel.city = e.labelArr[0]+city
			},
			//选择家乡
			selectArea(){
				this.$refs.simpleAddressForArea.open()
			},
			//选择家乡回调
			onConfirmForArea(e) {
				console.log('选择家乡', e)
				var city = e.labelArr[1]=='市辖区'?'':e.labelArr[1]
				this.formModel.area = e.labelArr[0]+city  //.replace(/-/g,'，')
			},
			//单位选择
			selectUnit(){
				this.$Router.push({
					name: 'selectUnit'
				})
			},
			selectUnitBack(unit){
				this.dept = unit
				this.formModel.dept = unit.id
				this.formModel.unit = unit.unit
			},
			//提交表单
			submit() {
				this.updateUserInfo()
			},
			
			updateUserInfo(){
				let that = this
				user.updateUser(this.formModel).then(res => {
					if(res.data.code == 10000){
						this.userInfo = JSON.parse(JSON.stringify(res.data.data))
						uni.navigateBack()
						uni.showToast({
							title: '保存成功',
							icon: 'none'
						})
					}
				})
			},
			getUserInfo(){
				user.getUserInfo().then(res => {
					var userInfo = res.data.data
					this.userInfo = JSON.parse(JSON.stringify(userInfo))
					
					if(userInfo.gender == 'M'){
						userInfo.genderText = '男'
					}else if(userInfo.gender == 'W'){
						userInfo.genderText = '女'
					}
					userInfo.height = String(userInfo.height)
					this.formModel = userInfo
					
					this.defaultTime = this.formModel.birthday
				})
			},
			
		},
	}
</script>

<style lang="scss" scoped>
	.container{
		display: flex;
		flex-direction: column;
		padding: 0 30rpx;
		background-color: #FFFFFF;
		height: 100vh;
	}
	.footer{
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 100rpx;
	}
</style>
